package com.base.moduleAI.service.impl;

import com.base.core.common.utils.OkHttpClientUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import okhttp3.*;


import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

public class DeepSeekChatClient {

    private static final String API_URL = "https://api.deepseek.com/chat/completions";
    private static final String API_KEY = "sk-7ac8f6a5ada14c57a09afcc4de3b4033";
    /**
     * 调用DeepSeek Chat API
     * @param content 用户输入的消息内容（如："Hello!"）
     * @return API响应内容
     */
    public static String callDeepSeekChat(String content) throws Exception {

        // 1. 构建JSON请求体（自动处理转义）
        try {
            // 1. 构建JSON请求体
            ObjectMapper mapper = new ObjectMapper();
            ObjectNode requestBody = mapper.createObjectNode()
                    .put("model", "deepseek-chat")
                    .set("messages", mapper.createArrayNode()
                            .add(mapper.createObjectNode()
                                    .put("role", "system")
                                    .put("content", "You are a helpful assistant"))
                            .add(mapper.createObjectNode()
                                    .put("role", "user")
                                    .put("content", content))
                    );

            // 2. 构建请求头
            Map<String, String> headers = new HashMap<>();
            headers.put("Content-Type", "application/json");
            headers.put("Authorization", "Bearer " + API_KEY);
            headers.put("Accept-Charset", "charset=utf-8");
            // 3. 发送请求
            return OkHttpClientUtil.doPostJson(API_URL, requestBody.toString(), headers);
        } catch (Exception e) {
            System.err.println("调用DeepSeek API失败: " + e.getMessage());
            return "调用失败: " + e.getMessage();
        }

    }

    /**
     * JSON特殊字符转义
     */
    private static String escapeJson(String input) {
        return input.replace("\\", "\\\\")
                .replace("\"", "\\\"")
                .replace("\n", "\\n")
                .replace("\r", "\\r")
                .replace("\t", "\\t");
    }
// 替换为你的DeepSeek API Key
}
